package com.xci.platform.web.aspect;

import com.xci.platform.auth.service.LogService;
import com.xci.platform.helper.StringHelper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Web控制器执行日志
 */
@Slf4j
@Aspect
@Component
public class WebLogAspect {
    private ThreadLocal<Long> startTimeThreadLocal = new ThreadLocal<>();

    /**
     * 日志服务
     */
    @Resource
    private LogService logService;

    @Pointcut("execution(* com.xci.platform..*.controller..*.*(..))")
    public void executeLogPointcut() {
    }

    @Before("executeLogPointcut()")
    public void doBefore(JoinPoint joinPoint) {
        startTimeThreadLocal.set(System.currentTimeMillis());
    }

    @AfterReturning(pointcut = "executeLogPointcut()")
    public void doAfterReturning(JoinPoint joinPoint) {
        long usedTime = System.currentTimeMillis() - startTimeThreadLocal.get();
        // log.debug(StringHelper.format("耗时:{}ms", usedTime));
        // if (usedTime < 100) {
        //     return;
        // }
        // Object[] args = joinPoint.getArgs();
        // List<Object> list = new ArrayList<>();
        // for (Object obj : args) {
        //     if (obj == null) {
        //         continue;
        //     }
        //     if (obj instanceof HttpServletRequest || obj instanceof HttpServletResponse) {
        //         continue;
        //     }
        //     list.add(obj.toString());
        // }
        // String method = joinPoint.getSignature().getDeclaringType().getSimpleName() + "." + joinPoint.getSignature().getName();
        // String param = Joiner.on(",").join(list);
        // //访问日志入库
        // ServletRequestAttributes servlet = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        // if (servlet != null) {
        //     try {
        //         LogEntity entity = Util.buildLogInfo(servlet.getRequest(), null);
        //         entity.setOperateType(LogOperateType.Access.getId());
        //         entity.setModule("SpringMvc");
        //         entity.setCostTime(usedTime);
        //         entity.setResult(1);
        //         entity.setMessage("ok");
        //         entity.setDetails(Util.format("方法:{}  参数:{}", method, param));
        //
        //         // HttpSession session = servlet.getRequest().getSession();
        //         // Object user = session.getAttribute(WebConst.UserSessionKey);
        //         // if (user != null) {
        //         //     Util.setOperateUser(entity, (AuthUser) user);
        //         // }
        //         logService.insert(entity);
        //     } catch (Exception ex) {
        //         log.error("访问日志写入出错:" + ex.getMessage(), ex);
        //     }
        // }
        // log.debug(Util.format("方法:{},参数:{},耗时:{}ms", method, param, usedTime));
    }
}